/*******************************************************************************
 * Package: com.hngtrust.iir.report.service.impl
 * Type:    ReportCommentServiceImpl
 * Date:    2021/9/14 10:13
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.material.service.impl;


import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.elk.exception.ElkErrorType;
import com.hngtrust.iir.elk.exception.ElkException;
import com.hngtrust.iir.material.controller.dto.MaterialCommentInsertVO;
import com.hngtrust.iir.material.controller.dto.MaterialCommentQueryVO;
import com.hngtrust.iir.material.dto.MaterialCommentDTO;
import com.hngtrust.iir.material.entity.MaterialComment;
import com.hngtrust.iir.material.service.MaterialCommentBacService;
import com.hngtrust.iir.material.service.MaterialCommentService;

import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.util.ElkUtils;
import com.tiger.util.MyHttpClient;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 内部研报-我的评论
 *
 * @author Yujiaqi
 * @date 2021/9/14 10:13
 */
@Service
@Slf4j
public class MaterialCommentBacServiceImpl implements MaterialCommentBacService {
    

    @Autowired
    private MyIdGenerator myIdGenerator;
    @Autowired
    private MaterialCommentService materialCommentService;

    //文件路径
    @Value("${elk.search.url}")
    private String elkUrl;
    
    @Value(("${elk.search.indexname.means}"))
    private String fileName;
    
    /**
     * 内部研报-我的评论列表
     * @param materialCommentQueryVO
     * @return
     */
    @Override
    public List<MaterialCommentDTO> listComment(MaterialCommentQueryVO materialCommentQueryVO) {
        MaterialComment materialComment = new MaterialComment();
        materialComment.setReportId(materialCommentQueryVO.getReportId());
        List<MaterialCommentDTO> materialCommentDTOS = materialCommentService.queryMaterialComList(materialComment);
        return materialCommentDTOS;
    }
    
    /**
     * 内部资料-我的评论新增
     * @param materialCommentInsertVO
     */
    @Override
    public void addComment(MaterialCommentInsertVO materialCommentInsertVO) {
        MaterialComment materialComment = new MaterialComment();
        materialComment.setId(myIdGenerator.nextId());
        materialComment.setReportId(materialCommentInsertVO.getReportId());
        materialComment.setComment(materialCommentInsertVO.getComment());
        materialComment.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        materialComment.setCreateTime(new Date());
        materialComment.setUpdateTime(new Date());
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        materialComment.setCreateUserId(loginUser.getId());
        materialComment.setUpdateUserId(loginUser.getId());
        materialCommentService.add(materialComment);
        Long count = materialCommentService.countByReportId(materialComment.getReportId());
        //修改内部资料主表（评论数量）
        materialCommentService.updateComments(materialComment.getReportId(),count);
        
        String url;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(fileName)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, fileName);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + fileName + "/_update_by_query";
        } else {
            url = elkUrl + "/" + fileName + "/_update_by_query";
        }
        MyHttpClient client = new MyHttpClient();
        JSONConfig config = new JSONConfig();
        config.setOrder(true);
        JSONObject params = new JSONObject(config);
       
        JSONObject source = new JSONObject();
        source.put("source","ctx._source.iir['iir_comments'] = '"+count+"'");
        JSONObject m = new JSONObject();
        JSONObject f= new JSONObject();
        f.put("iir.iir_mysql_id",materialComment.getReportId());
        m.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,f);
        params.put("script",source);
        params.put("query",m);
        log.info("修改参数{}",params.toString());
        client.postJson(url,params);
    }
}
